package com.ggh.jinjilive.ui.login;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.UserMobile;
import com.ggh.jinjilive.mobile.login.LoginMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;
import com.tencent.qcloud.tim.uikit.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginViewModel extends BaseViewModel {
    //手机号
    public ObservableField<String> mobile = new ObservableField<>("");
    //密码
    public ObservableField<String> password = new ObservableField<>("");
    //
    public MutableLiveData<UserMobile> userMobileMutableLiveData = new MutableLiveData<>();

    public LoginViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }


    public void login(){
        Map<String, Object> map = new HashMap<>();
        map.put("mobile",mobile.get());
        map.put("password",password.get());
        addSubscribe(apiCallInterface.login(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LoginMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LoginMobile loginMobile = (LoginMobile) resultEntity.getData();
                if(null != loginMobile){
                    //标记登录
                    MerchantSession.getInstance().setIsLogin(true);
                    //token
                    MerchantSession.getInstance().setToken(loginMobile.getToken());
                    //设置手机号
                    MerchantSession.getInstance().setMobile(mobile.get());
                    //存储密码
                    MerchantSession.getInstance().setPassword(password.get());
                    //获取用户数据
                    mydata(loginMobile.getUser_id());
                }
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                ToastUtils.make().show(netMsg);
                userMobileMutableLiveData.setValue(null);
            }
        }));

    }

    /**
     * 获取用户数据
     * @param user_id
     */
    public void mydata(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        addSubscribe(apiCallInterface.mydata(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(new InterceptObserver<UserMobile>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                UserMobile userMobile = (UserMobile) resultEntity.getData();
                userMobileMutableLiveData.setValue(userMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                userMobileMutableLiveData.setValue(null);
            }
        }));
    }
}
